//
//  CYCheckCenterVController.m
//  CreditAddressBook
//
//  Created by Lee on 15/4/30.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYCheckCenterVController.h"
#import "CYHttpTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYEscape.h"
#import "CYUser.h"
#import "MJExtension.h"
#import "UIButton+WebCache.h"
#import "CYLoginHttpTool.h"
#import "CYJumpsTool.h"
#import "LEImagePicker.h"
#import "LEProgressButton.h"
#import "CustomIOSAlertView.h"
#import "CYJsonInfo.h"
#import "CYGetdealCodeVC.h"
#import "CYJumpsTool.h"
#import "AddressView.h"

@interface CYCheckCenterVController ()<UIScrollViewDelegate,UIPickerViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *idCardTF;
@property (weak, nonatomic) IBOutlet UITextField *bankTF;
@property (weak, nonatomic) IBOutlet UITextField *bankCardTF;
@property (weak, nonatomic) IBOutlet UITextField *bankProTF;
@property (weak, nonatomic) IBOutlet UITextField *bankDetailTF;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, strong) NSArray *pickdata;
@property (nonatomic, strong) AddressView *addressView;
//地区选择器
@property (nonatomic, strong) UIToolbar *addresstoolBar;
@property (nonatomic, strong) UIPickerView *addresspicker;
@property (nonatomic, strong) AddressView *selectaddressView;

@property (nonatomic, strong) NSString *provincestr;
@property (nonatomic, strong) NSString *citystr;
@property (nonatomic, strong) NSString *areastr;


@end

@implementation CYCheckCenterVController

-(void)viewWillAppear:(BOOL)animated{
    [self setupNavigationView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _provincestr = [[NSString alloc]init];
    _citystr = [[NSString alloc]init];
    _areastr = [[NSString alloc]init];
    self.title = @"实名认证";
    _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.height/1.5,self.view.width,self.view.height/3 )];
    //    指定Delegate
    _picker.delegate=self;
    _idCardTF.delegate = self;
    _bankCardTF.delegate = self;
    _bankDetailTF.delegate = self;
    _bankProTF.delegate = self;
    _bankTF.delegate = self;
    _submitBtn.layer.cornerRadius = 6.0f;
    NSArray *dataArray = [[NSArray alloc]initWithObjects:@"中国工商银行",@"中国农业银行",@"中国银行",@"中国建设银行",@"交通银行",@"招商银行",@"中国邮政储蓄银行",@" 中信银行",@"招商银行",@"中国光大银行",@"华夏银行",@"中国民生银行",@"广东广发银行",@"兴业银行",@"上海浦东发展银行",@"平安银行",@"浙商银行",@"上海银行",nil];
    _pickdata=dataArray;
    _picker.showsSelectionIndicator=YES;
    _bankTF.inputView = self.picker;
    _bankTF.inputAccessoryView = self.toolBar;
    
    _addresspicker =[[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.height/1.5,self.view.width,self.view.height/3 )];
    _addresspicker.delegate = self;
    self.selectaddressView = [[AddressView alloc] init];
    self.bankProTF.inputView = self.selectaddressView;
    self.bankProTF.inputAccessoryView = self.addresstoolBar;
    
}
- (UIToolbar *)addresstoolBar{
    if (_addresstoolBar == nil) {
        self.addresstoolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
        _addresstoolBar.barTintColor= CYColor(95, 200, 100, 1);
        UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
        _addresstoolBar.items = @[item1, item2];
    }
    return _addresstoolBar;
}

- (void)click {
    if (_bankProTF.isFirstResponder) {
        [_bankProTF resignFirstResponder];
        self.bankProTF.text = [NSString stringWithFormat:@"%@%@%@",self.selectaddressView.province,self.selectaddressView.city,self.selectaddressView.area];
        _provincestr = self.selectaddressView.province;
        _citystr = self.selectaddressView.city;
        _areastr = self.selectaddressView.area;

        //            NSLog(@"text = %@",_areaText);
           }
    if([_bankProTF.text  isEqual: @"(null)(null)(null)"]) {
        _provincestr = @"北京市";
        _citystr = @"北京辖区";
        _areastr = @"东城区";
        _bankProTF.text = [NSString stringWithFormat:@"%@%@%@",_provincestr,_citystr,_areastr];
    }
}


- (void)setupNavigationView {
    UIView *naviView = [[UIView alloc] init];
    naviView.backgroundColor = CYNaviThemeColor;
    naviView.frame = CGRectMake(0, 0, self.view.width, 64);
    [self.view addSubview:naviView];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
    [_nameTF resignFirstResponder];
    [_idCardTF resignFirstResponder];
    [_bankTF resignFirstResponder];
    [_bankProTF resignFirstResponder];
    [_bankDetailTF resignFirstResponder];
    [_bankCardTF resignFirstResponder];
    return YES;
}


//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_pickdata count];
}

#pragma mark Picker Delegate Methods

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_pickdata objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _bankTF.text = _pickdata[row];
    
}
- (UIToolbar *)toolBar{
    if (_toolBar == nil) {
        self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
        _toolBar.barTintColor= CYColor(103, 190, 102, 1);
        
        UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        //        UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithTitle:@"请选择银行" style:UIBarButtonItemStylePlain target:self action:nil];
        UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(confirm)];
        _toolBar.items = @[item1, item2];
    }
    return _toolBar;
}
- (void)confirm {
    if ([_bankTF.text isEqualToString:@""]) {
        self.bankTF.text = @"中国工商银行";
    }
    [self.bankTF resignFirstResponder];
}

- (BOOL)checkSuccess {
    if (!_nameTF||!_nameTF.text.length||!_idCardTF||!_idCardTF.text.length||!_bankTF||!_bankTF.text.length||!_bankProTF||!_bankProTF.text.length||!_bankCardTF||!_bankCardTF.text.length) {
        CYAlertView(@"请填写全信息");
        return NO;
    }
    return YES;
}


- (IBAction)submitClick:(id)sender {
    if ([self checkSuccess]) {
        CYLog(@"province = %@ , city = %@",_provincestr,_citystr);
        CYUser *user = [CYLoginHttpTool userData].user;
        if (!_bankDetailTF.text||!_bankDetailTF.text.length) {
            _bankDetailTF.text = @"";
        }
        
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYHttpTool postWithURL:[CYURL shared].checkCertificateNoAndBandCard params:@{@"userId":user.userId,@"token":user.token,@"cardType":_bankTF.text,@"province":_provincestr,@"city":_citystr,@"cardNo":_bankCardTF.text,@"saleNetwork":_bankDetailTF.text,@"name":_nameTF.text,@"certificateNo":_idCardTF.text} success:^(id json) {
            [MBProgressHUD hideHUDForView:self.view];
            CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
            if (jsonInfo.isSuccess) {
                [MBProgressHUD showSuccess:jsonInfo.message];
                if (user) {
                    user.name = self.nameTF.text;
                    user.certificateNo = self.idCardTF.text;
                    //将最新的用户信息保存到沙盒
                    [CYLoginHttpTool updateUserData:user];
                }
                //跳转到上一层界面并设置完善资料按钮隐藏
                //认证成功绑定交易密码
                CYUser *user = [CYLoginHttpTool userData].user;
                [CYHttpTool postWithURL:[CYURL shared].getDealCodeStatus params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
                    CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
                    NSString  *type = jsonInfo.data;
                    if (type.intValue == 1) {
                        [MBProgressHUD hideHUDForView:self.view];
                        [CYJumpsTool jumpToMain:self.view];

                    }else{
                        [MBProgressHUD hideHUDForView:self.view];
                        CYGetdealCodeVC *getde = [[CYGetdealCodeVC alloc]init];
                        [self.navigationController pushViewController:getde animated:YES];

                    }
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view];
                    CYLog(@"error = %@",error);
                }];

            }else {
                [MBProgressHUD hideHUDForView:self.view];
                [MBProgressHUD showError:jsonInfo.message];
            }
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            CYLog(@"ERROR = %@",error);
        }];
    }
}

- (void)dealloc {
    CYLog(@"完善个人资料销毁");
}

@end
